package com.chinags.system.report.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.chinags.common.annotation.Log;
import com.chinags.common.constant.UserConstants;
import com.chinags.common.core.domain.AjaxResult;
import com.chinags.common.core.domain.Ztree;
import com.chinags.common.enums.BusinessType;
import com.chinags.common.utils.StringUtils;
import com.chinags.system.report.domain.TJcFlzddwCount;
import com.chinags.system.report.mapper.TJcFlzddwCountMapper;

import com.chinags.system.report.service.ITJcFlzddwCountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinags.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 省市区 服务层实现
 * 
 * @author shichenxing
 * @date 2019-06-22
 */
@Service
public class TJcFlzddwCountServiceImpl implements ITJcFlzddwCountService {
	@Autowired
	private TJcFlzddwCountMapper tJcFlzddwCountMapper;

	/**
	 * 查询省市区信息
	 *
	 * @param createId 省市区ID
	 * @return 省市区信息
	 */
	@Override
	public TJcFlzddwCount selectTJcFlzddwCountById(Long createId) {
		return tJcFlzddwCountMapper.selectTJcFlzddwCountById(createId);
	}

	/**
	 * 查询省市区列表
	 *
	 * @param tJcFlzddwCount 省市区信息
	 * @return 省市区集合
	 */
	@Override
	public List<TJcFlzddwCount> selectTJcFlzddwCountList(TJcFlzddwCount tJcFlzddwCount) {
		return tJcFlzddwCountMapper.selectTJcFlzddwCountList(tJcFlzddwCount);
	}

	/**
	 * 新增省市区
	 *
	 * @param tJcFlzddwCount 省市区信息
	 * @return 结果
	 */
	@Override
	public int insertTJcFlzddwCount(TJcFlzddwCount tJcFlzddwCount) {
		return tJcFlzddwCountMapper.insertTJcFlzddwCount(tJcFlzddwCount);
	}

	/**
	 * 修改省市区
	 *
	 * @param tJcFlzddwCount 省市区信息
	 * @return 结果
	 */
	@Override
	public int updateTJcFlzddwCount(TJcFlzddwCount tJcFlzddwCount) {
		return tJcFlzddwCountMapper.updateTJcFlzddwCount(tJcFlzddwCount);
	}

	/**
	 * 删除省市区对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteTJcFlzddwCountByIds(String ids) {
		return tJcFlzddwCountMapper.deleteTJcFlzddwCountByIds(Convert.toStrArray(ids));
	}


	@Override
//	@DataScope(tableAlias = "d")
	public List<Ztree> selecitDeptTree(TJcFlzddwCount dept) {
		List<TJcFlzddwCount> deptList = tJcFlzddwCountMapper.selectDeptList(dept);
		List<Ztree> ztrees = initZtree(deptList);
		return ztrees;
	}

	/**
	 * 对象转部门树
	 *
	 * @param deptList 部门列表
	 * @return 树结构列表
	 */
	public List<Ztree> initZtree(List<TJcFlzddwCount> deptList) {
		return initZtree(deptList, null);
	}

	/**
	 * 对象转部门树
	 *
	 * @param deptList     部门列表
	 * @param roleDeptList 角色已存在菜单列表
	 * @return 树结构列表
	 */
	public List<Ztree> initZtree(List<TJcFlzddwCount> deptList, List<String> roleDeptList) {

		List<Ztree> ztrees = new ArrayList<Ztree>();
		boolean isCheck = StringUtils.isNotNull(roleDeptList);
		for (TJcFlzddwCount dept : deptList) {
			if (UserConstants.DEPT_NORMAL.equals(dept.getStatus())) {
				Ztree ztree = new Ztree();
				ztree.setId(dept.getCreateId());
				ztree.setpId(dept.getPrentId());
				ztree.setName(dept.getEname());
				ztree.setTitle(dept.getEname());
				if (isCheck) {
					ztree.setChecked(roleDeptList.contains(dept.getCreateId() + dept.getEname()));
				}
				ztrees.add(ztree);
			}
		}
		return ztrees;
	}

	/**
	 * 根据部门ID查询信息
	 *
	 * @param createId 部门ID
	 * @return 部门信息
	 */
	@Override
	public TJcFlzddwCount selectDeptById(Long createId) {
		return tJcFlzddwCountMapper.selectDeptById(createId);

	}
	/**
	 * 校验部门名称是否唯一
	 *
	 * @param dept 部门信息
	 * @return 结果
	 */
	@Override
	public String checkDeptNameUnique(TJcFlzddwCount dept)
	{
		Long deptId = StringUtils.isNull(dept.getCreateId()) ? -1L : dept.getCreateId();
		TJcFlzddwCount info = tJcFlzddwCountMapper.checkDeptNameUnique(dept.getEname(), dept.getPrentId());
		if (StringUtils.isNotNull(info) && info.getCreateId().longValue() != deptId.longValue())
		{
			return UserConstants.DEPT_NAME_NOT_UNIQUE;
		}
		return UserConstants.DEPT_NAME_UNIQUE;
	}
	/**
	 * 修改保存部门信息
	 *
	 * @param dept 部门信息
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateDept(TJcFlzddwCount dept)
	{
		TJcFlzddwCount newParentDept = tJcFlzddwCountMapper.selectDeptById(dept.getPrentId());
		TJcFlzddwCount oldDept = selectDeptById(dept.getCreateId());
		if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
		{
			String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getCreateId();
			String oldAncestors = oldDept.getAncestors();
			dept.setAncestors(newAncestors);
			updateDeptChildren(dept.getCreateId(), newAncestors, oldAncestors);
		}
		int result = tJcFlzddwCountMapper.updateTJcFlzddwCount(dept);
		if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
		{
			// 如果该部门是启用状态，则启用该部门的所有上级部门
			updateParentDeptStatus(dept);
		}
		return result;
	}
	/**
	 * 修改子元素关系
	 *
	 * @param deptId 被修改的部门ID
	 * @param newAncestors 新的父ID集合
	 * @param oldAncestors 旧的父ID集合
	 */
	public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors)
	{
		List<TJcFlzddwCount> children = tJcFlzddwCountMapper.selectChildrenDeptById(deptId);
		for (TJcFlzddwCount child : children)
		{
			child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
		}
		if (children.size() > 0)
		{
			tJcFlzddwCountMapper.updateDeptChildren(children);
		}
	}
	/**
	 * 修改该部门的父级部门状态
	 *
	 * @param dept 当前部门
	 */
	private void updateParentDeptStatus(TJcFlzddwCount dept)
	{
		String updateBy = dept.getUpdateBy();
		dept = tJcFlzddwCountMapper.selectDeptById(dept.getCreateId());
		dept.setUpdateBy(updateBy);
		tJcFlzddwCountMapper.updateDeptStatus(dept);
	}











}
